#!/usr/bin/env python
from math import hypot, atan2

def dot(Ax, Ay, Bx, By):
    """Dot product of two vectors in 2d"""
    return Ax * Bx + Ay * By

def getDirection(Ax, Ay, Bx, By):
    """Direction from A to B in radians"""
    return atan2(By - Ay, Bx - Ax)

def pointOnLine(Px, Py, Ax, Ay, Bx, By):
    """Point projection on a line. Readable version. Pretty Straightforward."""
    Vx = Px - Ax
    Vy = Py - Ay
    Cx = Bx - Ax
    Cy = By - Ay
    d = dot(Vx, Vy, Cx, Cy) / (Cx ** 2 + Cy ** 2)
    return Ax + Cx * d, Ay + Cy * d

def pointOnSegment(Px, Py, Ax, Ay, Bx, By):
    """Point projection on a line. Readable version. Pretty Straightforward."""
    Vx = Px - Ax
    Vy = Py - Ay
    Cx = Bx - Ax
    Cy = By - Ay
    d = dot(Vx, Vy, Cx, Cy) / (Cx ** 2 + Cy ** 2)
    d = max(0, min(1, d))
    return Ax + Cx * d, Ay + Cy * d

def pointOnCircle(Px, Py, Cx, Cy, Cr):
    dx = Px - Cx
    dy = Py - Cy
    distance = hypot(dx, dy)
    return Cx + Cr / distance * dx, Cy + Cr / distance * dy

def circleFromRect(rect):
    yb = rect.top
    a, b = rect.size
    y = yb + (4 * a**2 + b**2) / (8 * a)
    r = (4 * a**2 + b**2) / (8 * a)
    return rect.centerx, y, r

def trilateration(Ax, Ay, Bx, By, Cx, Cy):
    """Returns the circle which intersects A B and C"""
    pass